import { defHttp } from '/@/utils/http/axios';
import { useMessage } from '/@/hooks/web/useMessage';

const { createConfirm } = useMessage();

enum Api {
  list = '/contracts/contracts/list',
  save = '/contracts/contracts/add',
  edit = '/contracts/contracts/edit',
  deleteOne = '/contracts/contracts/delete',
  deleteBatch = '/contracts/contracts/deleteBatch',
  importExcel = '/contracts/contracts/importExcel',
  exportXls = '/contracts/contracts/exportXls',
  approve = '/contracts/contracts/approve',
  reject = '/contracts/contracts/reject',
  contractAmountInfoList = '/contracts/contracts/listContractAmountInfoByMainId',
  contractAmountInfoSave = '/contracts/contracts/addContractAmountInfo',
  contractAmountInfoEdit = '/contracts/contracts/editContractAmountInfo',
  contractAmountInfoDelete = '/contracts/contracts/deleteContractAmountInfo',
  contractAmountInfoDeleteBatch = '/contracts/contracts/deleteBatchContractAmountInfo',
  contractDetailInfoList = '/contracts/contracts/listContractDetailInfoByMainId',
  contractDetailInfoSave = '/contracts/contracts/addContractDetailInfo',
  contractDetailInfoEdit = '/contracts/contracts/editContractDetailInfo',
  contractDetailInfoDelete = '/contracts/contracts/deleteContractDetailInfo',
  contractDetailInfoDeleteBatch = '/contracts/contracts/deleteBatchContractDetailInfo',
  // 新增文件上传相关接口
  uploadFile = '/contracts/contracts/uploadFile',
  getFiles = '/contracts/contracts/getFiles',
  deleteFile = '/contracts/contracts/deleteFile',
  previewFile = '/contracts/contracts/previewFile',
  downloadFile = '/contracts/contracts/downloadFile',
  getDetail = '/contracts/contracts/getDetail',
  applyForInvoice = '/contracts/contracts/applyForInvoice',
  getInvoiceFiles = '/FinancialInvoice/financialInvoice//inContractsGetFiles',
  downloadInvoiceFile = '/FinancialInvoice/financialInvoice/downloadFile',
}
/**
 * 导出api
 * @param params
 */
export const getExportUrl = Api.exportXls;

/**
 * 导入api
 */
export const getImportUrl = Api.importExcel;

/**
 * 列表接口
 * @param params
 */
export const list = (params) => defHttp.get({ url: Api.list, params });

/**
 * 删除单个
 */
export const deleteOne = (params, handleSuccess) => {
  return defHttp.delete({ url: Api.deleteOne, params }, { joinParamsToUrl: true }).then(() => {
    handleSuccess();
  });
};

/**
 * 批量删除
 * @param params
 */
export const batchDelete = (params, handleSuccess) => {
  createConfirm({
    iconType: 'warning',
    title: '确认删除',
    content: '是否删除选中数据',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      return defHttp.delete({ url: Api.deleteBatch, data: params }, { joinParamsToUrl: true }).then(() => {
        handleSuccess();
      });
    },
  });
};

/**
 * 保存或者更新
 * @param params
 */
export const saveOrUpdate = (params, isUpdate) => {
  const url = isUpdate ? Api.edit : Api.save;
  return defHttp.post({ url: url, params }, { isTransformResponse: false });
};

/**
 * 审批接口
 * @param contractId 合同ID
 * @param approveData 审批数据
 */
export const approveContract = (contractId: string, approveData: any) => {
  console.log('审批接口调用:', {
    url: `${Api.approve}/${contractId}`,
    params: approveData,
    contractId,
  });

  return defHttp
    .get(
      {
        url: `${Api.approve}/${contractId}`,
        params: approveData, // 使用params传递查询参数
      },
      { isTransformResponse: false }
    )
    .catch((error) => {
      console.error('审批接口错误详情:', {
        error,
        url: `${Api.approve}/${contractId}`,
        params: approveData,
        status: error?.response?.status,
        statusText: error?.response?.statusText,
        data: error?.response?.data,
      });
      throw error;
    });
};

/**
 * 拒绝审批接口
 * @param contractId 合同ID
 * @param rejectData 拒绝数据
 */
export const rejectContract = (contractId: string, rejectData: any) => {
  console.log('拒绝审批接口调用:', {
    url: `${Api.reject}/${contractId}`,
    params: rejectData,
    contractId,
  });

  return defHttp
    .get(
      {
        url: `${Api.reject}/${contractId}`,
        params: rejectData, // 使用params传递查询参数
      },
      { isTransformResponse: false }
    )
    .catch((error) => {
      console.error('拒绝审批接口错误详情:', {
        error,
        url: `${Api.reject}/${contractId}`,
        params: rejectData,
        status: error?.response?.status,
        statusText: error?.response?.statusText,
        data: error?.response?.data,
      });
      throw error;
    });
};

/**
 * 列表接口
 * @param params
 */
export const contractAmountInfoList = (params) => {
  if (params['contractNumber']) {
    return defHttp.get({ url: Api.contractAmountInfoList, params });
  }
  return Promise.resolve({});
};

/**
 * 删除单个
 */
export const contractAmountInfoDelete = (params, handleSuccess) => {
  return defHttp.delete({ url: Api.contractAmountInfoDelete, params }, { joinParamsToUrl: true }).then(() => {
    handleSuccess();
  });
};

/**
 * 批量删除
 * @param params
 */
export const contractAmountInfoDeleteBatch = (params, handleSuccess) => {
  createConfirm({
    iconType: 'warning',
    title: '确认删除',
    content: '是否删除选中数据',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      return defHttp.delete({ url: Api.contractAmountInfoDeleteBatch, data: params }, { joinParamsToUrl: true }).then(() => {
        handleSuccess();
      });
    },
  });
};

/**
 * 保存或者更新
 * @param params
 */
export const contractAmountInfoSaveOrUpdate = (params, isUpdate) => {
  let url = isUpdate ? Api.contractAmountInfoEdit : Api.contractAmountInfoSave;
  return defHttp.post({ url: url, params }, { isTransformResponse: false });
};

/**
 * 导入
 */
export const contractAmountInfoImportUrl = '/contracts/contracts/importContractAmountInfo';

/**
 * 导出
 */
export const contractAmountInfoExportXlsUrl = '/contracts/contracts/exportContractAmountInfo';

/**
 * 列表接口
 * @param params
 */
export const contractDetailInfoList = (params) => {
  if (params['contractNumber']) {
    return defHttp.get({ url: Api.contractDetailInfoList, params });
  }
  return Promise.resolve({});
};

/**
 * 删除单个
 */
export const contractDetailInfoDelete = (params, handleSuccess) => {
  return defHttp.delete({ url: Api.contractDetailInfoDelete, params }, { joinParamsToUrl: true }).then(() => {
    handleSuccess();
  });
};

/**
 * 批量删除
 * @param params
 */
export const contractDetailInfoDeleteBatch = (params, handleSuccess) => {
  createConfirm({
    iconType: 'warning',
    title: '确认删除',
    content: '是否删除选中数据',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      return defHttp.delete({ url: Api.contractDetailInfoDeleteBatch, data: params }, { joinParamsToUrl: true }).then(() => {
        handleSuccess();
      });
    },
  });
};

/**
 * 保存或者更新
 * @param params
 */
export const contractDetailInfoSaveOrUpdate = (params, isUpdate) => {
  let url = isUpdate ? Api.contractDetailInfoEdit : Api.contractDetailInfoSave;
  return defHttp.post({ url: url, params }, { isTransformResponse: false });
};

/**
 * 导入
 */
export const contractDetailInfoImportUrl = '/contracts/contracts/importContractDetailInfo';

/**
 * 导出
 */
export const contractDetailInfoExportXlsUrl = '/contracts/contracts/exportContractDetailInfo';

/**
 * 获取合同详情（包含文件）
 * @param id 合同ID
 */
export const getContractDetail = (id: string) => {
  return defHttp.get(
    {
      url: Api.getDetail,
      params: { id },
    },
    { isTransformResponse: false }
  );
};

/**
 * 获取合同文件列表
 * @param contractId 合同ID
 */
export const getContractFiles = (contractId: string) => {
  return defHttp.get(
    {
      url: Api.getFiles,
      params: { contractId },
    },
    { isTransformResponse: false }
  );
};

/**
 * 上传合同文件
 * @param file 文件
 * @param contractId 合同ID
 */
export const uploadContractFile = (file, contractId) => {
  return defHttp.post(
    {
      url: Api.uploadFile,
      params: { file, contractId },
    },
    { joinParamsToUrl: true, isTransformResponse: false }
  );
};

/**
 * 预览文件
 * @param fileId 文件ID
 */
export const previewFile = (fileId) => {
  return defHttp.get(
    {
      url: Api.previewFile,
      params: { fileId },
      responseType: 'blob'
    },
    { joinParamsToUrl: true, isTransformResponse: false }
  );
};

/**
 * 下载文件
 * @param fileId 文件ID
 */
export const downloadFile = (fileId) => {
  return defHttp.get(
    {
      url: Api.downloadFile,
      params: { fileId },
      responseType: 'blob'
    },
    { joinParamsToUrl: true, isTransformResponse: false }
  );
};

/**
 * 删除合同文件
 * @param fileId 文件ID
 */
export const deleteContractFile = (fileId) => {
  return defHttp.delete(
    {
      url: Api.deleteFile,
      params: { fileId },
    },
    { joinParamsToUrl: true, isTransformResponse: false }
  );
};

/**
 * 获取产品类型列表（产品类型名称和ID）
 */
export const getProductTypeList = () => {
  return defHttp.get({
    url: '/products/productType/getProductTypeNameAndId',
    params: {}
  });
};

/**
 * 创建团队
 */
export const createTeam = (params) => {
  return defHttp.post({
    url: '/teams/teams/createTeam',
    params
  });
};

/**
 * 根据产品类型ID获取产品名称列表
 * @param productTypeId 产品类型ID
 */
export const getProductContentList = (productTypeId) => {
  return defHttp.get({
    url: '/products/productType/getProductNameByProductTypeId',
    params: { productTypeId: productTypeId }
  });
};

/**
 * 申请发票
 * @param contractData 合同数据
 */
export const applyForInvoice = (contractData) => {
  return defHttp.post({
    url: Api.applyForInvoice,
    data: contractData
  });
};

/**
 * 获取合同发票文件列表
 * @param contractId 合同ID
 */
export const getInvoiceFiles = (contractId) => {
  return defHttp.get({
    url: Api.getInvoiceFiles,
    params: { id: contractId }  // 后端用id接收参数
  });
};

/**
 * 下载发票文件
 * @param invoiceFileId 发票文件ID
 */
export const downloadInvoiceFile = (invoiceFileId) => {
  return defHttp.get({
    url: Api.downloadInvoiceFile,
    params: { fileId: invoiceFileId },
    responseType: 'blob'
  });
};
